package com.example.demo;


import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DemoTest {

    public static void main(String[] args) throws UnknownHostException {



        // on startup

      /* TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                //.addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
                .addTransportAddress(new TransportAddress(InetAddress.getByName("http://129.28.177.161"), 9300));*/



       /* Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("129.28.177.161"), 9300));*/

        //外网要放开 9300端口
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("129.28.177.161"), 9300));

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //搜索条件
         builder.withQuery(QueryBuilders.matchAllQuery());

           // builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));

        //聚合搜索品牌名称
      //  builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));

       //  builder.addAggregation(aggregationBuilder);
      /*  NativeSearchQuery searchQuery = builder.build();
          elasticsearchTemplate.query(searchQuery, response -> {
            LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
            return convertProductRelatedInfo(response);
        });*/



      /*  client.admin().indices().prepareCreate("twitter")
                .setSettings(Settings.builder()
                        .put("index.number_of_shards", 3)
                        .put("index.number_of_replicas", 2)
                )
                .get();*/


        // on shutdown


        System.out.println("ending--------------------------->");


//        client.close();
    }
}
